package yjh.mapper;

import org.apache.ibatis.annotations.*;
import yjh.pojo.MistakesTopic;

import java.util.Date;
import java.util.List;

@Mapper
public interface MistakesTopicMapper {
    // 增加错题
    @Insert("INSERT INTO mistakestopic VALUES(null,#{topicid},#{mistakescount}," +
            "#{reviewtime})")
    public boolean insertMistakesTopic(@Param("topicid")int tid,
                                       @Param("mistakescount") int mistakescount,
                                       @Param("reviewtime") Date reviewtime);

    //根据题库中的题目删除错题
    @Delete("DELETE FROM mistakestopic WHERE " +
            "topicid = (SELECT id FROM topictable WHERE topic = #{topic})")
    public boolean deleteMistakesTopic(@Param("topic") String topic);


//    @Delete("DELETE FROM mistakestopic WHERE " +
//            "topicid = #{tid}")
//    public boolean deleteMistakesTopicByTid(@Param("tid") int tid);

    //根据id删除错题
    @Delete("DELETE FROM mistakestopic WHERE id = #{mid}")
    public boolean deleteMistakesTopicByid(@Param("mid") int mid);

    //修改错题
    @Update("UPDATE mistakestopic SET mistakescount=#{mistakescount}, " +
            "reviewtime=#{reviewtime} WHERE topicid = (SELECT id " +
            "FROM topictable WHERE topic = #{topic})")
    public boolean updateMistakesTopicByTopic(@Param("topic") String topic,
                                              @Param("mistakescount") int mistakescount,
                                              @Param("reviewtime")Date reviewtime);

    //查询全部错题
    @Select("SELECT * FROM mistakestopic")
    public List<MistakesTopic> selectMistakesTopicAll();

    //根据tid查询错题
    @Select("SELECT * FROM mistakestopic WHERE topicid =#{tid}")
    public MistakesTopic selectMistakesTopicByTopicID(@Param("tid") int tid);

    //根据mid查询错题
    @Select("SELECT * FROM mistakestopic WHERE mid =#{mid}")
    public MistakesTopic selectMistakesTopicByMid(@Param("mid") int mid);

    //查询错题个数
    @Select("SELECT count(*) FROM mistakestopic")
    public int selectMistakesTopicByTopicCount();

    //查询指定时间段错题
    @Select("SELECT * FROM mistakestopic WHERE reviewtime <= " +
            "(SELECT min(reviewtime) FROM mistakestopic) + #{date} AND reviewtime >= " +
            "(SELECT min(reviewtime) FROM mistakestopic) ")
    public List<MistakesTopic> selectMistakesTopicByDate(@Param("date") int date);

    //查询指定错误次数范围内的
    @Select("SELECT * FROM mistakestopic WHERE mistakescount>=#{count1} AND mistakescount<=#{count2}")
    public List<MistakesTopic> selectMistakesTopicByMistakescount(@Param("count1") int count1,
                                                                  @Param("count2") int count2);

    //查询最多的错误次数
    @Select("SELECT max(mistakescount) FROM mistakestopic")
    public int selectMistakesTopicMax();



}
